package com.atguigu.gmall.realtime.sink;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.realtime.bean.TableProcess;
import com.atguigu.gmall.realtime.common.Constant;
import com.atguigu.gmall.realtime.util.JdbcUtil;
import com.atguigu.gmall.realtime.util.RedisUtil;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.functions.sink.RichSinkFunction;
import redis.clients.jedis.Jedis;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Map;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/9/29 10:20
 * 通过jdbc来实现
 */
public class PhoenixSink extends RichSinkFunction<Tuple2<JSONObject, TableProcess>> {
    
    private Connection conn;
    private ValueState<Boolean> isFirstState;
    private Jedis redisClient;
    
    //
    @Override
    public void open(Configuration parameters) throws Exception {
        // 创建Phoenix连接
        // 1. 加载Phoenix驱动
        
        // 2. 获取连接对象
        String url = Constant.PHOENIX_URL;
        conn = JdbcUtil.getPhoenixConnection(url);
        
        isFirstState = getRuntimeContext().getState(new ValueStateDescriptor<Boolean>("isFirstState", Boolean.class));
    
        redisClient = RedisUtil.getClient();
    
    }
    
    // 流中每来一条数据, 执行一次
    @Override
    public void invoke(Tuple2<JSONObject, TableProcess> value,
                       Context ctx) throws Exception {
        // 1. 检测要写的表是否存在, 不存在就需要先建表
        checkTable(value);
        // 2. 写入数据
        writeToPhoenix(value);
        // 3. 更新缓存
        updateCache(value);
        
    }
    
    private void updateCache(Tuple2<JSONObject, TableProcess> value) {
        
        JSONObject dim = value.f0;
        TableProcess tp = value.f1;
    
        String key = tp.getSink_table() + ":" + dim.getLong("id");
    
        JSONObject upperDim = new JSONObject();
        if ("update".equals(tp.getOperate_type()) && redisClient.exists(key)) {
            // 去更新缓存: 缓存存在才需要更新
            // 把dim中所有的字段名全部大写
            for (Map.Entry<String, Object> entry : dim.entrySet()) {
                upperDim.put(entry.getKey().toUpperCase(), entry.getValue());
            }
            redisClient.set(key, upperDim.toJSONString());
        }
    
        // 粗暴
        //redisClient.del(key);
    }
    
    private void writeToPhoenix(Tuple2<JSONObject, TableProcess> value) throws SQLException {
        JSONObject data = value.f0;
        TableProcess tp = value.f1;
        
        // 1. 拼接sql   "id,name"
        // upsert into t(id, name) values(?,?)
        StringBuilder sql = new StringBuilder();
        sql
            .append("upsert into ")
            .append(tp.getSink_table())
            .append("(")
            // 字段
            .append(tp.getSink_columns())
            .append(")values(")
            .append(tp.getSink_columns().replaceAll("[^,]+", "?"))
            .append(")");
        
        // 2. 获取PrepareStatement
        PreparedStatement ps = conn.prepareStatement(sql.toString());
        // 3. 给sql中的占位符赋值
        String[] columnNames = tp.getSink_columns().split(",");
        for (int i = 0; i < columnNames.length; i++) {
            Object v = data.get(columnNames[i]); // v有可能是null,
            // 1 -> "1"    null -> "null"
            ps.setString(i + 1, v == null ? null : v.toString());
        }
        System.out.println("插入语句: " + sql.toString());
        // 4. 执行
        ps.execute();
        conn.commit();
        ps.close();
        
    }
    
    private void checkTable(Tuple2<JSONObject, TableProcess> value) throws SQLException, IOException {
        
        if (isFirstState.value() == null) {
            System.out.println("xxxx");
            // 建表只需要第一条数据过来的时候执行一次
            TableProcess tp = value.f1;
            // 主要工作建表
            // create table if not exists t(id varchar, name varchar, constraint pk primary key(id, name)) SALT_BUCKETS = 4;
            StringBuilder sql = new StringBuilder();
            sql
                .append("create table if not exists ")
                .append(tp.getSink_table())
                .append("(")
                .append(tp.getSink_columns().replaceAll(",", " varchar,"))
                .append(" varchar, constraint pk primary key(")
                .append(tp.getSink_pk() == null ? "id" : tp.getSink_pk())
                .append("))")
                .append(tp.getSink_extend() == null ? "" : tp.getSink_extend());
            
            PreparedStatement ps = conn.prepareStatement(sql.toString());
            ps.execute();
            conn.commit();
            ps.close();
            
            isFirstState.update(true);
        }
        
    }
    
    @Override
    public void close() throws Exception {
        if (conn != null) {
            conn.close();
        }
    }
}
